package com.jd.myaaaccount.model;

import java.math.BigDecimal;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import com.jd.myaaaccount.LoginActivity;
import com.jd.myaaaccount.entity.Host;
import com.jd.myaaaccount.entity.result.BaseResult;
import com.jd.myaaaccount.util.AlertDialogHelper;
import com.jd.myaaaccount.util.ErrorCode;
import com.jd.myaaaccount.util.OperateCode;

public class Common {

	public static String token;
	public static Host currentUser;

	/**
	 * 检测当前用户是否已经登录 或登录过期
	 * 
	 * @param context
	 * @return
	 */
	public static void isLogin(Context context, Handler handler) {
		Message msg = new Message();
		msg.what = OperateCode.CHECK_LOGIN;
		if (currentUser == null) {
			SharedPreferences sp = context.getSharedPreferences("user",
					Context.MODE_PRIVATE);
			token = sp.getString("token", "");

			if (!token.equals("")) {// 已经登录
				currentUser = new Host();
				currentUser.setId(sp.getString("id", ""));
				currentUser.setName(sp.getString("name", ""));
				currentUser.setNumber(sp.getInt("number", 0));
			}
		}
		msg.arg1 = currentUser == null ? ErrorCode.NOT_LOGIN
				: ErrorCode.SUCCESS;
		handler.sendMessage(msg);
	}

	/**
	 * 登录超期 跳转到登录页面
	 */
	public static void loginTimeOut(final Activity context) {
		// 清除本地缓存
		cleanLoginInfo(context);

		new AlertDialog.Builder(context).setTitle("提示").setMessage("登录过期")
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						Intent intent = new Intent();
						intent.setClass(context, LoginActivity.class);
						context.startActivity(intent);
						context.finish();
					};
				}).show();

	}

	public static void cleanLoginInfo(final Activity context) {
		SharedPreferences sp = context.getSharedPreferences("user",
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		editor.putString("token", "");
		editor.commit();
		Common.token = "";
		Common.currentUser = null;
	}

	/**
	 * 将结果转换成Message
	 * 
	 * @param res
	 * @param msg
	 */
	public static void resultToMessage(BaseResult res, Message msg) {
		if (msg == null)
			msg = new Message();
		msg.arg1 = res.getResult();
		if (msg.arg1 == ErrorCode.SUCCESS) {
			msg.obj = res.getContent();
		} else {
			msg.arg1 = res.getErrorCode();
			msg.obj = res.getErrorMsg();
		}
	}

	/**
	 * 显示错误消息
	 * 
	 * @param msg
	 *            默认显示的信息
	 * @param flag
	 *            0 toast方式显示 1 alert方式显示
	 */
	public static void showErrorInfo(Activity context, int errorCode,
			String msg, int flag) {
		String errorMsg = "";
		switch (errorCode) {
		case ErrorCode.NET_TIMEOUT:
			errorMsg = "网络异常";
			break;
		case ErrorCode.EMAIL_OR_PWD_WRONG:
			errorMsg = "用户名或密码错误";
			break;
		case ErrorCode.EMAIL_EXIST:
			errorMsg = "邮箱已存在";
			break;
		case ErrorCode.FAMILY_OR_PWD_WRONG:
			errorMsg = "家庭不存在或者密码错误";
			break;
		case ErrorCode.FAMILY_MEMBER_EXIST:
			errorMsg = "重复加入";
			break; 
		case ErrorCode.TOKEN_OVERDUE:
			loginTimeOut(context);
			return; 
		case ErrorCode.SYS_ERROR:
			errorMsg = "系统错误";
			break;
		default:
			if (msg.equals("")) {
				errorMsg = "系统错误:" + errorCode;
			} else {
				errorMsg = msg + ":" + errorCode;
			}
			break;
		}

		if (flag == 0)
			Toast.makeText(context, errorMsg, Toast.LENGTH_SHORT).show();
		else if (flag == 1) {
			AlertDialogHelper.showOKAlertDialog(context, errorMsg);
		} else {
		}
	}

	/**
	 * 将double 保留指定小数位
	 * 
	 * @param value
	 *            原数据
	 * @param len
	 *            小数位数
	 * @return
	 */
	public static double convertDouble(double value, int len) {
		BigDecimal bd = new BigDecimal(value);
		return bd.setScale(len, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
}
